home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Bin / DXUtils / Visual Studio 6.0 Wizards / Source Code / Template / d3dutil.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-12  |  7.0 KB  |  177 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DUtil.h
  3. //
  4. // Desc: Helper functions and typing shortcuts for Direct3D programming.
  5. //-----------------------------------------------------------------------------
  6. #ifndef D3DUTIL_H
  7. #define D3DUTIL_H
  8. #include <D3D9.h>
  9. #include <D3DX9Math.h>
  10.  
  11.  
  12.  
  13.  
  14. //-----------------------------------------------------------------------------
  15. // Name: D3DUtil_InitMaterial()
  16. // Desc: Initializes a D3DMATERIAL9 structure, setting the diffuse and ambient
  17. //       colors. It does not set emissive or specular colors.
  18. //-----------------------------------------------------------------------------
  19. VOID D3DUtil_InitMaterial( D3DMATERIAL9& mtrl, FLOAT r=0.0f, FLOAT g=0.0f,
  20.                                                FLOAT b=0.0f, FLOAT a=1.0f );
  21.  
  22.  
  23.  
  24.  
  25. //-----------------------------------------------------------------------------
  26. // Name: D3DUtil_InitLight()
  27. // Desc: Initializes a D3DLIGHT structure, setting the light position. The
  28. //       diffuse color is set to white, specular and ambient left as black.
  29. //-----------------------------------------------------------------------------
  30. VOID D3DUtil_InitLight( D3DLIGHT9& light, D3DLIGHTTYPE ltType,
  31.                         FLOAT x=0.0f, FLOAT y=0.0f, FLOAT z=0.0f );
  32.  
  33.  
  34.  
  35.  
  36. //-----------------------------------------------------------------------------
  37. // Name: D3DUtil_CreateTexture()
  38. // Desc: Helper function to create a texture. It checks the root path first,
  39. //       then tries the DXSDK media path (as specified in the system registry).
  40. //-----------------------------------------------------------------------------
  41. HRESULT D3DUtil_CreateTexture( LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strTexture,
  42.                                LPDIRECT3DTEXTURE9* ppTexture,
  43.                                D3DFORMAT d3dFormat = D3DFMT_UNKNOWN );
  44.  
  45.  
  46.  
  47.  
  48. //-----------------------------------------------------------------------------
  49. // Name: D3DUtil_GetCubeMapViewMatrix()
  50. // Desc: Returns a view matrix for rendering to a face of a cubemap.
  51. //-----------------------------------------------------------------------------
  52. D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace );
  53.  
  54.  
  55.  
  56.  
  57. //-----------------------------------------------------------------------------
  58. // Name: D3DUtil_GetRotationFromCursor()
  59. // Desc: Returns a quaternion for the rotation implied by the window's cursor
  60. //       position.
  61. //-----------------------------------------------------------------------------
  62. D3DXQUATERNION D3DUtil_GetRotationFromCursor( HWND hWnd,
  63.                                               FLOAT fTrackBallRadius=1.0f );
  64.  
  65.  
  66.  
  67.  
  68. //-----------------------------------------------------------------------------
  69. // Name: D3DUtil_SetDeviceCursor
  70. // Desc: Builds and sets a cursor for the D3D device based on hCursor.
  71. //-----------------------------------------------------------------------------
  72. HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE9 pd3dDevice, HCURSOR hCursor,
  73.                                  BOOL bAddWatermark );
  74.  
  75.  
  76. //-----------------------------------------------------------------------------
  77. // Name: D3DUtil_D3DFormatToString
  78. // Desc: Returns the string for the given D3DFORMAT.
  79. //       bWithPrefix determines whether the string should include the "D3DFMT_"
  80. //-----------------------------------------------------------------------------
  81. TCHAR* D3DUtil_D3DFormatToString( D3DFORMAT format, bool bWithPrefix = true );
  82.  
  83.  
  84. //-----------------------------------------------------------------------------
  85. // Name: class CD3DArcBall
  86. // Desc:
  87. //-----------------------------------------------------------------------------
  88. class CD3DArcBall
  89. {
  90.     INT            m_iWidth;   // ArcBall's window width
  91.     INT            m_iHeight;  // ArcBall's window height
  92.     FLOAT          m_fRadius;  // ArcBall's radius in screen coords
  93.     FLOAT          m_fRadiusTranslation; // ArcBall's radius for translating the target
  94.  
  95.     D3DXQUATERNION m_qDown;               // Quaternion before button down
  96.     D3DXQUATERNION m_qNow;                // Composite quaternion for current drag
  97.     D3DXMATRIXA16  m_matRotation;         // Matrix for arcball's orientation
  98.     D3DXMATRIXA16  m_matRotationDelta;    // Matrix for arcball's orientation
  99.     D3DXMATRIXA16  m_matTranslation;      // Matrix for arcball's position
  100.     D3DXMATRIXA16  m_matTranslationDelta; // Matrix for arcball's position
  101.     BOOL           m_bDrag;               // Whether user is dragging arcball
  102.     BOOL           m_bRightHanded;        // Whether to use RH coordinate system
  103.  
  104.     D3DXVECTOR3 ScreenToVector( int sx, int sy );
  105.  
  106. public:
  107.     LRESULT     HandleMouseMessages( HWND, UINT, WPARAM, LPARAM );
  108.  
  109.     D3DXMATRIX* GetRotationMatrix()         { return &m_matRotation; }
  110.     D3DXMATRIX* GetRotationDeltaMatrix()    { return &m_matRotationDelta; }
  111.     D3DXMATRIX* GetTranslationMatrix()      { return &m_matTranslation; }
  112.     D3DXMATRIX* GetTranslationDeltaMatrix() { return &m_matTranslationDelta; }
  113.     BOOL        IsBeingDragged()            { return m_bDrag; }
  114.  
  115.     VOID        SetRadius( FLOAT fRadius );
  116.     VOID        SetWindow( INT w, INT h, FLOAT r=0.9 );
  117.     VOID        SetRightHanded( BOOL bRightHanded ) { m_bRightHanded = bRightHanded; }
  118.  
  119.                 CD3DArcBall();
  120.     VOID        Init();
  121. };
  122.  
  123.  
  124.  
  125.  
  126. //-----------------------------------------------------------------------------
  127. // Name: class CD3DCamera
  128. // Desc:
  129. //-----------------------------------------------------------------------------
  130. class CD3DCamera
  131. {
  132.     D3DXVECTOR3 m_vEyePt;       // Attributes for view matrix
  133.     D3DXVECTOR3 m_vLookatPt;
  134.     D3DXVECTOR3 m_vUpVec;
  135.  
  136.     D3DXVECTOR3 m_vView;
  137.     D3DXVECTOR3 m_vCross;
  138.  
  139.     D3DXMATRIXA16  m_matView;
  140.     D3DXMATRIXA16  m_matBillboard; // Special matrix for billboarding effects
  141.  
  142.     FLOAT       m_fFOV;         // Attributes for projection matrix
  143.     FLOAT       m_fAspect;
  144.     FLOAT       m_fNearPlane;
  145.     FLOAT       m_fFarPlane;
  146.     D3DXMATRIXA16  m_matProj;
  147.  
  148. public:
  149.     // Access functions
  150.     D3DXVECTOR3 GetEyePt()           { return m_vEyePt; }
  151.     D3DXVECTOR3 GetLookatPt()        { return m_vLookatPt; }
  152.     D3DXVECTOR3 GetUpVec()           { return m_vUpVec; }
  153.     D3DXVECTOR3 GetViewDir()         { return m_vView; }
  154.     D3DXVECTOR3 GetCross()           { return m_vCross; }
  155.  
  156.     FLOAT       GetFOV()             { return m_fFOV; }
  157.     FLOAT       GetAspect()          { return m_fAspect; }
  158.     FLOAT       GetNearPlane()       { return m_fNearPlane; }
  159.     FLOAT       GetFarPlane()        { return m_fFarPlane; }
  160.  
  161.     D3DXMATRIX  GetViewMatrix()      { return m_matView; }
  162.     D3DXMATRIX  GetBillboardMatrix() { return m_matBillboard; }
  163.     D3DXMATRIX  GetProjMatrix()      { return m_matProj; }
  164.  
  165.     VOID SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
  166.                         D3DXVECTOR3& vUpVec );
  167.     VOID SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
  168.                         FLOAT fFarPlane );
  169.  
  170.     CD3DCamera();
  171. };
  172.  
  173.  
  174.  
  175.  
  176. #endif // D3DUTIL_H
  177.